"""
自定义异常类
"""
from typing import Optional, Any
from fastapi import HTTPException

class BaseAPIException(HTTPException):
    """基础API异常"""
    def __init__(
        self,
        status_code: int,
        error_code: str,
        message: str,
        detail: Optional[Any] = None
    ):
        super().__init__(status_code=status_code, detail={
            "error_code": error_code,
            "message": message,
            "detail": detail
        })

class NotFoundException(BaseAPIException):
    """资源未找到异常"""
    def __init__(self, resource: str, detail: Optional[Any] = None):
        super().__init__(
            status_code=404,
            error_code="NOT_FOUND",
            message=f"{resource} not found",
            detail=detail
        )

class BadRequestException(BaseAPIException):
    """请求参数异常"""
    def __init__(self, message: str, detail: Optional[Any] = None):
        super().__init__(
            status_code=400,
            error_code="BAD_REQUEST",
            message=message,
            detail=detail
        )

class ConflictException(BaseAPIException):
    """资源冲突异常"""
    def __init__(self, message: str, detail: Optional[Any] = None):
        super().__init__(
            status_code=409,
            error_code="CONFLICT",
            message=message,
            detail=detail
        )

class InternalServerException(BaseAPIException):
    """服务器内部异常"""
    def __init__(self, message: str = "Internal server error", detail: Optional[Any] = None):
        super().__init__(
            status_code=500,
            error_code="INTERNAL_ERROR",
            message=message,
            detail=detail
        )